<template>
  <div class="home" style="margin-left: 5px;width: auto">
<!--    搜索区域-->
    <div style="margin: 10px">
      <el-input v-model="search" placeholder="请输入要搜索的内容" style="width: 20%" clearable/>
      <el-button type="primary" style="margin-left: 5px" @click="pageList">搜索</el-button>
    </div>
    <!--    功能区域-->
    <div style="margin: 10px">
      <el-button type="primary" @click="add">新增角色</el-button>
      <el-popconfirm title="确认删除吗?" @confirm="deleteByIds">
        <template #reference>
          <el-button type="danger">批量删除</el-button>
        </template>
      </el-popconfirm>
    </div>
<!--    数据区域-->
    <el-table :data="tableData" border stripe style="width: calc(100vw - 205px)">
      <el-table-column type="selection" width="55" />
      <el-table-column :resizable="false" prop="id" label="id" />
      <el-table-column :resizable="false" prop="name" label="角色" />
      <el-table-column :resizable="false" prop="description" label="描述" />

      <el-table-column fixed="right" label="操作" width="165px">
        <template #default="scope">
<!--          修改按钮-->
          <el-button @click="handleEdit(scope.row)">修改</el-button>
<!--          删除按钮-->
          <el-popconfirm title="确认删除吗?" @confirm="handleDelet(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
<!--    分页插件-->
    <div style="margin: 10px">
      <el-pagination
          :currentPage="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 9]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
<!--    新增的模态窗口-->
    <el-dialog
        v-model="dialogVisible"
        title="新增类型"
        width="30%"
    >
<!--      窗口内的表单-->
        <el-form :model="form" label-width="120px">
          <el-form-item label="角色名称">
            <el-input style="width: 80%" v-model="form.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input style="width: 80%" v-model="form.description" />
          </el-form-item>
        </el-form>
<!--新增的模态窗口的确认按钮-->
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {
    //注册

  },
  data() {
    return {
      form:{},
      dialogVisible:false,
      search:'',
      total:10,
      currentPage:1,
      pageSize:5,
      tableData:[],
      ids:[]
    }
  },
  created() {
    this.pageList()
  },
  methods:{
    deleteByIds() {
      console.log(this.ids)
      if(!this.ids.length){
        this.$message.error("请选择要删除的数据!");
        return
      }
      //批量删除
      request.post("order/role/deleteByIds/",this.ids).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSelectionChange(val) {
      this.ids=val.map(v => v.id);
    },
    pageList(){
      request.get("order/role/pageList", {
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          name: this.search
        }
      }).then(res =>{
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    save(){
      //新增菜品类型
      if(this.form.id == null){
        request.post("order/role/save", this.form).then(res =>{
          this.msgTip("新增",res);
        });
      }else{
        //修改菜品类型
        request.put("order/role/save", this.form).then(res =>{
          this.msgTip("修改",res);
        });
      }
      this.pageList();
      this.dialogVisible=false;
    },
    add(){
      //打开新增模态窗口
      this.dialogVisible = true;
      this.form = {};
    },
    handleEdit(row){
      //打开修改模态窗口
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelet(id){
      //删除
      request.delete("order/role/delete/" + id).then(res => {
        this.msgTip("删除",res);
        this.pageList();
      })
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize;
      this.pageList()
    },
    handleCurrentChange(currentPage){
      this.currentPage=currentPage;
      this.pageList()
    },
    msgTip(msg,res){
      if(res.code == 200){
        this.$message({
          type:"success",
          message: msg+"成功"
        })
      }else{
        this.$message({
          type:"error",
          message:res.msg
        })
      }
    }
  }
}
</script>
